/*
 * @Author: 1453937336@qq.com 1453937336@qq.com
 * @Date: 2024-04-28 12:27:15
 * @LastEditors: 1453937336@qq.com 1453937336@qq.com
 * @LastEditTime: 2024-06-12 11:48:44
 * @FilePath: \itforce-hris\src\utils\auth.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import Cookies from 'js-cookie'
import { Session } from './storage';

const TokenKey = 'itf-web-token'

export function getToken(): any {
  const tokenName = Session.get('userInfo')?.tokenName ?? ""
  if (tokenName) {
    return Cookies.get(tokenName);
  } else {
    console.error('tokenName为空,请检查！');
    return;
  }
}

export function setToken(token: any, time: any) {
  const date = new Date();
  date.setTime(date.getTime() + time ? 1 : time * 60 * 60 * 1000);
  return Cookies.set(TokenKey, token, time);
}

export function removeToken() {
  return Cookies.remove(TokenKey)
}

export function getCookies(cookieName: string): string {
  // const cookies = document.cookie;
  const cookies = decodeURIComponent(document.cookie);
  // // consolelog('cookies:', cookies);

  if (!cookies) return "";
  const list = cookies.split(";");
  for (let i = 0; i < list.length; i++) {
    const getValue = list[i].split("=");
    for (let j = 0; j < getValue.length; j++) {
      const key = getValue[0].replace(" ", "");
      const value = getValue[1];
      // // consolelog('value:', value, 'key:', key, 'getToken():', getToken());
      if (getToken() === value) {
        return true;
      }
    }
  }
  return "";
  // // consolelog('document.cookie:', document.cookie);

  // const cookieValue: any = document.cookie
  //     .split(';')
  //     .find(row => row.startsWith(cookieName + '='))
  //     ?.split('=')[1];

  // return cookieValue
}

/**
 * 这里设置cookie
 */
// export const setCookie = (value: any, { time = 1, path = "/" }) => {
export const setCookie = (name: String, value: String, days: number) => {
  // let date = new Date();
  // date.setTime(date.getTime() + time * 60 * 60 * 1000);
  // let cookieStr =
  //     TokenKey +
  //     "=" +
  //     value +
  //     ";" +
  //     "path=" +
  //     path +
  //     ";" +
  //     "expires=" +
  //     date.toUTCString();
  // // consolelog('cookieStr', cookieStr)
  // // Cookies.set(TokenKey, cookieStr)
  // Cookies.set(cookieStr, TokenKey)
  // document.cookie = cookieStr
  // // consolelog('你存到了吗?', document.cookie)
  // ---------------------------------------------------
  let expires = "";
  if (days) {
    const date = new Date();
    // date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000);
    date.setTime(date.getTime() + (days * 1000));
    expires = "; expires=" + date.toUTCString();
  }
  document.cookie = name + "=" + (value || "") + expires + "; path=/";
};


export const removeCookie = (key: string, { path = "/" }) => {
  const date = "Thu, 01 Jan 1970 00:00:00 GMT";
  const cookie = `${key}=;path=${path};expires=${date};`;
  document.cookie = cookie;
};

// 改主题色
export const changeTheme = (theme: string) => {
  const app = document.querySelector("body");
  app.dataset.theme = theme;
  app.className = theme;
};
